package com.tcode.util;

import java.sql.Connection;

import javax.sql.DataSource;

public class ConnectionUtil {
	
	ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
	
	private DataSource dataSource;
	
	
	
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}



	public Connection getThreadLocalConnection(){
		try{
			Connection con =threadLocal.get();
			if(con==null){
				con=dataSource.getConnection();
				threadLocal.set(con);
			}
			return con;
		}catch(Exception e){
			throw new RuntimeException();
		}
		
	}
	public void removeConnection(){
		threadLocal.remove();
	}

}
